/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import model.Contact;
/**
 * Form to add a contact
 * @author gramage
 */
public class FormAdd extends JFrame implements ActionListener{
    
    /**
     * Arraylist of contact
     */
    protected ArrayList al;
    
    /**
     * Main container
     */
    protected JPanel cont;

    /**
     * 
     */
    protected JLabel l_name;

    /**
     *
     */
    protected JLabel l_firstName;

    /**
     *
     */
    protected JLabel l_address;

    /**
     *
     */
    protected JLabel l_pPhone;

    /**
     *
     */
    protected JLabel l_proPhone;

    /**
     *
     */
    protected JLabel l_home;

    /**
     *
     */
    protected JLabel l_email;

    /**
     *
     */
    protected JLabel l_other;

    /**
     *
     */
    protected JTextField t_name;

    /**
     *
     */
    protected JTextField t_firstName;

    /**
     *
     */
    protected JTextField t_address;

    /**
     *
     */
    protected JTextField t_pPhone;

    /**
     *
     */
    protected JTextField t_proPhone;

    /**
     *
     */
    protected JTextField t_home;

    /**
     *
     */
    protected JTextField t_email;

    /**
     *
     */
    protected JTextField t_other;
    
    /**
     *
     */
    protected JButton button;

    /**
     * Constructor
     * @param ali Arraylist of contact
     */
    public FormAdd(ArrayList ali){
        super("Add a contact");
        this.setSize(220, 440);
        this.setDefaultCloseOperation(FormAdd.DISPOSE_ON_CLOSE);
        al=ali;
        // Container
        cont = new JPanel();
        cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));
        // Name
        l_name = new JLabel("Name :");
        cont.add(l_name);
        t_name = new JTextField(15);
        cont.add(t_name);

        // First name
        l_firstName = new JLabel("First Name :");
        cont.add(l_firstName);
        t_firstName = new JTextField(15);
        cont.add(t_firstName);
        
        // Address
        l_address =new JLabel("Address :");
        cont.add(l_address);
        t_address = new JTextField(15);
        cont.add(t_address);
        
        // Personnal phone
        l_pPhone =new JLabel("Phone :");
        cont.add(l_pPhone);
        t_pPhone = new JTextField(15);
        cont.add(t_pPhone);
        
        // Professional phone
        l_proPhone =new JLabel("Pro Phone :");
        cont.add(l_proPhone);
        t_proPhone = new JTextField(15);
        cont.add(t_proPhone);
        
        // Home
        l_home =new JLabel("Home :");
        cont.add(l_home);
        t_home = new JTextField(15);
        cont.add(t_home);
        
        // Mail
        l_email =new JLabel("Email :");
        cont.add(l_email);
        t_email = new JTextField(15);
        cont.add(t_email);
        
        // Other
        l_other =new JLabel("Other :");
        cont.add(l_other);
        t_other = new JTextField(15);
        cont.add(t_other);
        
        //Button
        button = new JButton("Add");
        button.addActionListener(this);
        cont.add(button);

        // Add container
        cont.setVisible(true);
        this.add(cont);        
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        //Save new contact
        if(e.getSource()==button){
            al.add(new Contact(t_name.getText(), t_firstName.getText(), t_address.getText(), t_pPhone.getText(), t_proPhone.getText(), t_home.getText(), t_email.getText(), t_other.getText()));
            this.dispose();
        }
    }

}
